﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EnvDTE;

namespace SPVisualDev
{
    internal partial class DialogSelectResource : Form
    {
        private ProjectItem m_feature_folder;
        private string m_feature_name;
        private bool m_add_new;

        public DialogSelectResource(ProjectItem FeatureFolder, bool bAddNew, string ResourceFileName)
        {
            InitializeComponent();

            m_add_new = bAddNew;
            m_feature_folder = FeatureFolder;
            m_feature_name = FeatureFolder.Name;
            this.ResourceFileName = ResourceFileName;

            this.rdoLocalResource.CheckedChanged += new EventHandler(rdoLocalResource_CheckedChanged);
            this.rdoGlobalResource.CheckedChanged += new EventHandler(rdoGlobalResource_CheckedChanged);
            this.FormClosing += new FormClosingEventHandler(DialogSelectResource_FormClosing);
        }

        private void DialogSelectResource_Load(object sender, EventArgs e)
        {
            this.Text = (m_add_new) ? "Create new resource file" : "Select existing resource file";

            if (m_add_new)
            {
                rdoLocalResource.Checked = true;
            }
            else
            {

                try
                {
                    ProjectItem resources = m_feature_folder.ProjectItems.Item("Resources").ProjectItems.Item("Resources.resx");

                    rdoLocalResource.Checked = true;
                }
                catch
                {
                    rdoLocalResource.Enabled = false;
                    rdoGlobalResource.Checked = true;
                }

/*
                if (string.Compare(this.ResourceFileName, Common.C_LOCAL_RESOURCE_IDENTIFIER, true) == 0 && rdoLocalResource.Enabled)
                {
                    rdoLocalResource.Checked = true;
                }
                else if (!string.IsNullOrEmpty(this.ResourceFileName))
                {
                    rdoGlobalResource.Checked = true;
                }
 */
            }
        }

        private void DialogSelectResource_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                bool isLocalResource = rdoLocalResource.Checked;

                if (this.DialogResult == DialogResult.OK)
                {
                    if (!isLocalResource)
                    {
                        if (m_add_new)
                        {
                            ResourceFileName = txtResourceFileName.Text;
                        }
                        else
                        {
                            if (cboSelectResourceFile.SelectedIndex < 1)
                                ResourceFileName = string.Empty;
                            else
                                ResourceFileName = cboSelectResourceFile.Text;
                        }
                    }
                    else
                    {
                        ResourceFileName = Common.C_LOCAL_RESOURCE_IDENTIFIER;
                    }


                    if (m_add_new)
                    {
                        ProjectItem resource_folder = null;

                        if (isLocalResource)
                        {
                            resource_folder = Common.CreateProjectItemPath(m_feature_folder.ContainingProject, "12\\TEMPLATE\\Features\\" + m_feature_folder.Name + "\\Resources");

                            try
                            {
                                ProjectItem resources = resource_folder.ProjectItems.Item("Resources.resx");
                                if (resources != null)
                                {
                                    MessageBox.Show("A resource file with the same name exists already. Operation aborted.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    return;
                                }
                            }
                            catch { }
                        }
                        else
                        {
                            resource_folder = Common.CreateProjectItemPath(m_feature_folder.ContainingProject, "12\\Resources");
                            try
                            {
                                ProjectItem resources = resource_folder.ProjectItems.Item(this.ResourceFileName + ".resx");
                                if (resources != null)
                                {
                                    MessageBox.Show("A resource file with the same name exists already. Operation aborted.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    return;
                                }
                            }
                            catch { }
                        }

                        if (resource_folder != null)
                        {
                            string res_name = (isLocalResource) ? "Resources.resx" : ResourceFileName + ".resx";
                            string template_path = ((EnvDTE80.Solution2)m_feature_folder.DTE.Solution).GetProjectItemTemplate("Resource.zip", "csproj");
                            resource_folder.ProjectItems.AddFromTemplate(template_path, res_name);

                            try
                            {
                                resource_folder.ProjectItems.Item(res_name).Document.Close(vsSaveChanges.vsSaveChangesYes);
                            }
                            catch { }

                            try
                            {
                                resource_folder.ProjectItems.Item("Resources.Resx").ProjectItems.Item("Resources.Designer.cs").Delete();
                            }
                            catch { }
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }
        }

        public string ResourceFileName
        {
            get;
            private set;
        }

        private void rdoLocalResource_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                RefreshList();

            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }
        }

        private void rdoGlobalResource_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                RefreshList();

            }
            catch (Exception ex)
            {
                DialogError.ShowError(ex);
            }
        }

        private void RefreshList()
        {
            if (rdoGlobalResource.Checked)
            {
                if (m_add_new)
                {
                    cboSelectResourceFile.Visible = false;
                    txtResourceFileName.Visible = true;
                    txtResourceFileName.Enabled = true;
                    txtResourceFileName.Text = Common.FilterIllegalCharsInFileName(m_feature_folder.ContainingProject.Name);
                }
                else
                {
                    cboSelectResourceFile.Visible = true;
                    cboSelectResourceFile.Enabled = true;
                    txtResourceFileName.Visible = false;

                    RefreshGlobalResourcesList();
                }

            }
            else if (rdoLocalResource.Checked)
            {
                if (m_add_new)
                {
                    cboSelectResourceFile.Visible = false;
                    txtResourceFileName.Visible = true;
                    txtResourceFileName.Enabled = false;
                    txtResourceFileName.Text = "<feature folder>\\Resources";
                }
                else
                {
                    cboSelectResourceFile.Visible = true;
                    cboSelectResourceFile.Items.Clear();
                    cboSelectResourceFile.Items.Add("<feature folder>\\Resources");
                    cboSelectResourceFile.SelectedIndex = 0;
                    cboSelectResourceFile.Enabled = false;
                    txtResourceFileName.Visible = false;
                }
            }
        }

        private void RefreshGlobalResourcesList()
        {
            cboSelectResourceFile.Items.Clear();
            cboSelectResourceFile.SelectedIndex = -1;

            ProjectItem resources = null;
            try
            {
                resources = m_feature_folder.ContainingProject.ProjectItems.Item("12").ProjectItems.Item("Resources");
            }
            catch { }

            if (resources != null)
            {
                foreach (ProjectItem item in resources.ProjectItems)
                {
                    string res_file_name = Path.GetFileNameWithoutExtension(item.Name);
                    string[] arr = res_file_name.Split('.');
                    if (arr.Length == 1)
                    {
                        int i = cboSelectResourceFile.Items.Add(arr[0]);
                        if (!string.IsNullOrEmpty(this.ResourceFileName))
                        {
                            if (string.Compare(this.ResourceFileName, arr[0], true) == 0)
                                cboSelectResourceFile.SelectedIndex = i;
                        }
                    }

                }
            }

            //if (cboSelectResourceFile.Items.Count == 0)
                cboSelectResourceFile.Items.Insert(0, "select resource file...");
    
            cboSelectResourceFile.Items.Insert(1, "core");
            
            if (cboSelectResourceFile.SelectedIndex == -1)
                cboSelectResourceFile.SelectedIndex = 0;

        }
    }
}
